#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2023/7/5 13:58
   @File: threadInsertBTA.py
"""
import threading
import queue
from queue import Empty
from DataTest.databases import MysqlUtil, DaMengUtil, OracleUtil
import datetime

from faker import Faker

from random import choice

from DataTest.sqlmontage.yanfeng_test import montage_sql
from DataTest.tabledata.commonData import randomInfo
from DataTest.tabledata.commonData.SQLMontage import read_file, get_sql_key, montage_sql_value
from DataTest.tabledata.commonData.clientInfo import client_info

fake = Faker(locale='zh_CN')

# 定义全局变量num_threads线程数量，num_inserts插入数据总量，batch_size单次插入数量
from DataTest.tabledata.commonData.SQLMontage import get_sql_key, get_sql, montage_sql_value
from DataTest.tabledata.commonData.clientInfo import client_info_hsraw

insert_number = 50
num_threads = 1
sql_number = 1
processes = []

# 定义队列用于存储拼接好的SQL语句
sql_queue = queue.Queue()

# 定义锁，用于保证线程安全
lock = threading.Lock()

BLOCK = True
# customerinfo_key = get_sql_key(table='taaml_n_customerinfo')
# customerinfo_value = get_sql(table='taaml_n_customerinfo')
# request_key = get_sql_key(table='taaml_n_request')
# request_value = get_sql(table='taaml_n_request')
# confirm_key = get_sql_key(table='taaml_n_confirm')
# confirm_value = get_sql(table='taaml_n_confirm')
# accorequest_key = get_sql_key(table='taaml_n_accorequest')
# accorequest_value = get_sql(table='taaml_n_accorequest')
# acconet_key = get_sql_key(table='taaml_n_acconet')
# acconet_value = get_sql(table='taaml_n_acconet')
# divide_key = get_sql_key(table='taaml_n_dividenddetail')
# divide_value = get_sql(table='taaml_n_dividenddetail')
# static_key = get_sql_key(table='taaml_n_staticshares')
# static_value = get_sql(table='taaml_n_staticshares')


# 定义线程执行的函数：SQL拼接
def sql_assemble(thread_id):
    startNum = 1
    endNum = 2
    for i in range(startNum, endNum):
        str = '000249770000'
        replacement = f"{i}"
        client_id = str[:-len(replacement)] + replacement
        # inner_client_id = client_id
        client_type = '0'
        sql_list = client_info()
        # client_type = choice(['1','2'])
        # sql_list = client_info()
        # client_name = fake.company() #名称
        # occupation_code = '1'

        # contact_address = '广东省韶关市酒厂街11号'
        #  id_type = '01'
        ta_no = '63'
        # apply_date = 20240621
        # open_date = 20240621
        # id_no = '511724200609256120'
        # nationality_code = 'CAF'
        # business_flag = '81'
        id_type = choice(['01', '02', '15', '05', '06', '07'])
        # occupation_code = choice(['37','34','36','15','2','1','14','16','17'])
        # sql_list.update({'client_name':client_name,'client_full_name':client_name})
        sql_list.update({'client_id': client_id, 'inner_client_id': client_id, 'client_type': client_type,
                         'ta_no': ta_no, 'id_type': id_type})
        # sql_list.update({'apply_date':apply_date,'open_date':open_date})
        montage_sql(table='fnd_client', sql_list=sql_list)
        montage_sql(table='fnd_fund_account', sql_list=sql_list)





# 定义线程执行的函数：SQL插入
def sql_insert(db):
    # while not sql_queue.empty():
    global sql
    while True:
        lock.acquire(blocking=True, timeout=-1)
        try:
            # print(sql_queue.qsize())
            sql = sql_queue.get(block=BLOCK)
            # print(f'插入sql:{sql}\n')
            # 执行SQL插入操作
        except Empty as e:
            break
        finally:
            lock.release()
            if sql:
                # print(sql)
                db.execsql(sql)
                print(f"{datetime.datetime.now()}插入成功:{sql}\n")

            else:
                break


if __name__ == '__main__':
    print(datetime.datetime.now())
    # 创建并启动SQL拼接线程
    assemble_threads = []
    for i in range(num_threads):
        thread = threading.Thread(target=sql_assemble, args=(i,))
        thread.start()
        assemble_threads.append(thread)

    # 创建并启动SQL插入线程
    insert_threads = []
    db = MysqlUtil('root', 'DFLTdf87220bf0', '10.20.181.116', port=33061)
    # db = MysqlUtil('root', 'fxq5@123', '10.20.46.217', port=33061)
    # db = OracleUtil('hsraw', 'fxq5@123', '10.20.37.111', server='bsaml')
    for i in range(num_threads):
        thread = threading.Thread(target=sql_insert, args=(db,))
        thread.start()
        insert_threads.append(thread)

    #等待所有SQL拼接线程执行完毕
    for thread in assemble_threads:
        thread.join()
    else:
        BLOCK = False
        sql_queue.put(False)

    # 等待所有SQL插入线程执行完毕
    # for thread in insert_threads:
    #     thread.join()
    db.commit()
    print(datetime.datetime.now())
